package com.pubinfo.passbook.api.controller.bill;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.api.service.ForwardFileService;
import com.pubinfo.passbook.common.entity.TSourceArtificial;
import com.pubinfo.passbook.common.mapper.TRuleUserWiteoffMapper;
import com.pubinfo.passbook.common.mapper.TSourceArtificialMapper;
import com.pubinfo.passbook.common.model.dto.MDM.TSourceArtificialParam;
import com.pubinfo.passbook.common.model.dto.file.ArtificialParam;
import com.pubinfo.passbook.common.model.dto.financial.PassbookParamAcct;
import com.pubinfo.passbook.common.model.ruler.PassbookInformation;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.base.MyPage;
import com.pubinfo.passbook.common.service.dubbo.DubboService;
import com.pubinfo.passbook.common.service.master.TPassbookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping(value = "Artifical")
@Api(tags = "人工报账")
@Slf4j
public class ArtificalController {
  @Autowired
  ForwardFileService forwardFileService;
  @Autowired
  TSourceArtificialMapper artificialMapper;
  
  @Autowired
  TRuleUserWiteoffMapper witeoffMapper;
  @Autowired
  DubboService dubboService;
  @Autowired
  TPassbookService tPassbookService;
  
  @PostMapping("saveArtificial")
  @ApiOperation(value = "保存人工审核数据")
  public String saveArtificial(ArtificialParam artificialParam) throws UnirestException {
    return forwardFileService.saveArtificial(artificialParam);
  }
  
  @PostMapping("selectArtificial")
  @ApiOperation(value = "查询人工审核数据")
  public String selectArtificialByCreateUser(@RequestBody TSourceArtificialParam param) throws UnirestException {
    return forwardFileService.selectArtificialByCreateUser(param);
  }
  
  @DeleteMapping("deleteArtificial")
  @ApiOperation(value = "删除指定人工报账")
  public JsonResult<Boolean> deleteArtificial(String id) {
    return JsonResult.success(artificialMapper.deleteByArtificialId(id));
  }
  
  @PostMapping("updateArtificial")
  @ApiOperation(value = "根据id修改人工报账")
  public JsonResult<Integer> updateArtificial(@RequestBody TSourceArtificial tSourceArtificial) {
    return JsonResult.success(artificialMapper.updateById(tSourceArtificial));
  }
  
  @GetMapping("countSumByAcct/{acct}/{classCode}")
  @ApiOperation(value = "根据用户账号查询历史报账总金额")
  public JsonResult<BigDecimal> countSumByAcct(@PathVariable("acct") String acct, @PathVariable("classCode") Integer classCode) {
    return JsonResult.success(witeoffMapper.countSumByAcct(acct, classCode));
  }
  
  @GetMapping("findStartLine/{acct}")
  @ApiOperation(value = "根据用户账号查询统筹支付金额")
  public JsonResult<BigDecimal> countSumByAcct(@PathVariable("acct") String acct) {
    String nsrsbh = dubboService.getUserNsrsbhByAcct(acct);
    return JsonResult.success(artificialMapper.findStartLineByNsrsbh(nsrsbh));
  }
  
  @GetMapping("findSum/{acct}")
  @ApiOperation(value = "根据用户账号查询当年未锁定票据总金额")
  public JsonResult<BigDecimal> findSum(@PathVariable("acct") String acct) {
    String nsrsbh = dubboService.getUserNsrsbhByAcct(acct);
    return JsonResult.success(artificialMapper.findStartLineByNsrsbh(nsrsbh));
  }

  @PostMapping("passbookInformationList")
  @ApiOperation(value = "查询该用户所有票据的详细信息")
  public JsonResult<MyPage<PassbookInformation>> passbookInformationList(@RequestBody PassbookParamAcct passbookParamAcct)
  {
    IPage<PassbookInformation> passbookInformationIPage=tPassbookService.selectTPassbookInformationByAcct(passbookParamAcct);
    passbookInformationIPage.getRecords().forEach(passbookInformation -> passbookInformation.setInformation());
    return JsonResult.success(new MyPage(passbookInformationIPage));
  }

}
